package com.mooke.controller;



import com.mooke.entity.*;
import com.mooke.service.impl.*;
import java.io.File;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminServiceImpl adminService;

    @Autowired
    private UserloginServiceImpl userloginServiceImpl;

    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<教职工操作>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

    // 教师页面显示
    @RequestMapping("/showTeacher.html")
    public String showTeacher(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {

        Integer curpage = 1;//从第一页开始
        Integer perpage = 10;//每页两条数据
        if (request.getParameter("curpage") != null) {
            curpage = Integer.parseInt(request.getParameter("curpage"));
        }
        ;//此处获取点击的页数

        String findByName = null;
        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("curpage", (curpage - 1) * perpage);
        map.put("perpage", perpage);

        if (!"".equals(request.getParameter("findByName")) && request.getParameter("findByName") != null) {
            findByName = request.getParameter("findByName");
            map.put("findByName", findByName);
            List<Teacher> list = adminService.findByNameTeacher("Admin.findByNameTeacher", map);
            model.addAttribute("list", list);
            model.addAttribute("username", username);
            return "admin/showTeacher";
        }

        List<Teacher> list = adminService.findByPagingTeacher("Admin.findByPagingTeacher", map);
        model.addAttribute("list", list);
        //查询数据库中总条数
        Integer totalcount = adminService.getCountTeacher("Admin.getCountTeacher", map);

        Integer totalpage = 0;
        if (totalcount % perpage == 0) {
            totalpage = totalcount / perpage;
        } else {
            totalpage = (totalcount / perpage) + 1;
        }

        model.addAttribute("totalcountStudent", totalcount);
        model.addAttribute("list", list);
        model.addAttribute("curpage", curpage);
        model.addAttribute("totalpage", totalpage);
        model.addAttribute("findByName", findByName);
        model.addAttribute("username", username);

        return "admin/showTeacher";

    }

    //  添加教师信息页面显示
    @RequestMapping(value = "/addTeacher.html", method = {RequestMethod.GET})
    public String addTeacher(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        List<College> list = adminService.finAll("Admin.finAll", map);
        model.addAttribute("list", list);
        model.addAttribute("adminname", username);
        return "admin/addTeacher";
    }

    // ajax校验学号是否存在
    @RequestMapping(value = "/checkTeacher.html", method = {RequestMethod.POST})
    @ResponseBody
    public String checkTeacher(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        String teacherID = request.getParameter("teacherID");
        Teacher teacher = new Teacher();
        teacher.setTeacherID(teacherID);
        Boolean result = adminService.checkTeacher("Admin.checkTeacher", teacher);
        //学号存在时
        if (result) {
            return "exist";
        }
        //学号不存在
        return "unexist";
    }

    // 添加教师信息操作
    @RequestMapping(value = "/addTeachered.html", method = {RequestMethod.POST})
    public String addTeachered(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String teacherID = request.getParameter("teacherID");
        String teacherName = request.getParameter("teacherName");
        String sex = request.getParameter("sex");
        String title = request.getParameter("title");
        String grade = request.getParameter("grade");
        String collegeid = request.getParameter("collegeid");
        Teacher teacher = new Teacher();
        teacher.setTeacherID(teacherID);
        teacher.setTeachername(teacherName);
        teacher.setSex(sex);
        teacher.setTitle(title);
        teacher.setGrade(grade);
        teacher.setCollegeName(collegeid);
        teacher.setRole("2");
        adminService.addTeacher("Admin.addTeacher", teacher);
        adminService.addTeacherLogin("Admin.addTeacherLogin", teacher);
        String adminname = request.getParameter("adminname");
        model.addAttribute("username", adminname);
        //重定向
        return "redirect:showTeacher.html";
    }

    // 进入修改教师信息处理页面
    @RequestMapping(value = "/editTeacher.html", method = {RequestMethod.GET})
    public String editTeacher(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String teacherID = request.getParameter("teacherID");
        String teacherName = request.getParameter("teacherName");
        String sex = request.getParameter("sex");
        String title = request.getParameter("title");
        String grade = request.getParameter("grade");
        String collegeid = request.getParameter("collegeid");
        model.addAttribute("teacherID", teacherID);
        model.addAttribute("teacherName", teacherName);
        model.addAttribute("sex", sex);
        model.addAttribute("title", title);
        model.addAttribute("grade", grade);
        model.addAttribute("collegeid", collegeid);
        Map<String, Object> map = new HashMap<String, Object>();
        List<College> list = adminService.finAll("Admin.finAll", map);
        model.addAttribute("list", list);
        return "admin/editTeacher";
    }

    // 修改老师信息的保存
    @RequestMapping(value = "/editTeachered.html", method = {RequestMethod.POST})
    public String editTeachered(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String teacherID = request.getParameter("teacherID");
        String teacherName = request.getParameter("teacherName");
        String sex = request.getParameter("sex");
        String title = request.getParameter("title");
        String grade = request.getParameter("grade");
        String collegeid = request.getParameter("collegeid");
        Teacher teacher = new Teacher();
        teacher.setTeacherID(teacherID);
        teacher.setTeachername(teacherName);
        teacher.setSex(sex);
        teacher.setTitle(title);
        teacher.setGrade(grade);
        teacher.setCollegeName(collegeid);
        adminService.updateTeacher("Admin.updateTeacher", teacher);
        //重定向
        return "redirect:showTeacher.html";
    }

    //删除教师
    @RequestMapping(value = "/removeTeacher.html", method = {RequestMethod.GET})
    public String removeTeacher(HttpServletRequest request, HttpServletResponse response, Model model) {
        String teacherID = request.getParameter("teacherID");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("teacherID", teacherID);
        adminService.removeTeacher("Admin.removeTeacher", map);
        map.put("teacherID", teacherID);
        adminService.removeLoginStudent("Admin.removeLoginStudent", map);
        return "redirect:showTeacher.html";
    }


    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<工资管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

    // 教师页面显示
    @RequestMapping("/showWages.html")
    public String showWages(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {


        Integer curpage = 1;//从第一页开始
        Integer perpage = 10;//每页两条数据
        if (request.getParameter("curpage") != null) {
            curpage = Integer.parseInt(request.getParameter("curpage"));
        }
        ;//此处获取点击的页数

        String findByName = null;
        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("curpage", (curpage - 1) * perpage);
        map.put("perpage", perpage);

        if (!"".equals(request.getParameter("findByName")) && request.getParameter("findByName") != null) {
            findByName = request.getParameter("findByName");
            map.put("findByName", findByName);
            List<Teacher> list = adminService.findByNameTeacher("Admin.findByNameTeacher", map);
            model.addAttribute("list", list);
            model.addAttribute("username", username);
            return "admin/showWages";
        }
        List<Teacher> list = adminService.findByPagingTeacher("Admin.findByPagingTeacher", map);
        model.addAttribute("list", list);
        //查询数据库中总条数
        Integer totalcount = adminService.getCountTeacher("Admin.getCountTeacher", map);

        Integer totalpage = 0;
        if (totalcount % perpage == 0) {
            totalpage = totalcount / perpage;
        } else {
            totalpage = (totalcount / perpage) + 1;
        }

        model.addAttribute("totalcountStudent", totalcount);
        model.addAttribute("list", list);
        model.addAttribute("curpage", curpage);
        model.addAttribute("totalpage", totalpage);
        model.addAttribute("findByName", findByName);
        model.addAttribute("username", username);

        return "admin/showWages";

    }

    @RequestMapping(value = "/addWages.html")
    public String addWages(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String username = request.getParameter("username");
        String teacherID = request.getParameter("teacherID");
        String teacherName = request.getParameter("teacherName");
        String month = request.getParameter("month");
        model.addAttribute("teacherID", teacherID);
        model.addAttribute("teacherName", teacherName);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("teacherID", teacherID);

        if (!"".equals(month) && month != null) {
            map.put("month", month);
            List<Teacher> list = adminService.leave("Admin.leave", map);
            List<Teacher> list2 = adminService.get("Admin.get", map);
            Integer Money = adminService.getCountMoney("Admin.getCountMoney", map);
           /* Boolean queryMoneyleave = adminService.queryMoneyleave("Admin.queryMoneyleave", map);
            Boolean queryMoneyget = adminService.queryMoneyget("Admin.queryMoneyget", map);*/
            Integer Moneyleave=0;
            Integer Moneyget=0;
            if(list.size()!=0){
                Moneyleave  = adminService.getCountMoneyleave("Admin.getCountMoneyleave", map);
            }
            if(list2.size()!=0){
                Moneyget = adminService.getCountMoneyget("Admin.getCountMoneyget", map);
            }
            String state = adminService.getState("Admin.getState", map);
            if ( Moneyleave == null) {
                Moneyleave=0;
            }
            if ( Moneyget == null) {
                Moneyget=0;
            }
            model.addAttribute("Money", Money);
            model.addAttribute("Moneyleave", Moneyleave);
            model.addAttribute("Moneyget", Moneyget);
            model.addAttribute("Salary", Money+Moneyget-Moneyleave);
            model.addAttribute("state", state);
            model.addAttribute("list", list);
            model.addAttribute("list2", list2);
            model.addAttribute("username", username);
            return "admin/addWages";
        }
        Date day= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String dateNowStr = sdf.format(day);
        map.put("month", dateNowStr);
        List<Teacher> list = adminService.leave("Admin.leave", map);
        List<Teacher> list2 = adminService.get("Admin.get", map);
        Integer Money = adminService.getCountMoney("Admin.getCountMoney", map);
        Integer Moneyleave=0;
        Integer Moneyget=0;
        if(list.size()!=0){
            Moneyleave  = adminService.getCountMoneyleave("Admin.getCountMoneyleave", map);
        }
        if(list2.size()!=0){
            Moneyget = adminService.getCountMoneyget("Admin.getCountMoneyget", map);
        }
        if ( Moneyleave == null) {
            Moneyleave=0;
        }
        if ( Moneyget == null) {
            Moneyget=0;
        }
        String state = adminService.getState("Admin.getState", map);
        model.addAttribute("Money", Money);
        model.addAttribute("Moneyleave", Moneyleave);
        model.addAttribute("Moneyget", Moneyget);
        model.addAttribute("Salary", Money+Moneyget-Moneyleave);
        model.addAttribute("state", state);
        model.addAttribute("list", list);
        model.addAttribute("list2", list2);
        model.addAttribute("username", username);
        return "admin/addWages";
    }

    //  发放工资后
    @RequestMapping(value = "/addWagesed.html")
    public String addWagesed(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        String teacherID = request.getParameter("teacherID");
        String salary = request.getParameter("salary");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("teacherID", teacherID);
        map.put("salary", salary);
        adminService.addWages("Admin.addWages", map);
        return "redirect:showWages.html";
    }


    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<院系部门管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

    // 部门页面显示
    @RequestMapping("/showCollege.html")
    public String showCollege(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {

        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        List<College> list = adminService.finAll("Admin.finAll", map);
        model.addAttribute("list", list);
        model.addAttribute("username", username);
        return "admin/showCollege";
    }

    // 添加部门页面
    @RequestMapping("/addCollege.html")
    public String addCollege(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {
        String username = request.getParameter("username");
        model.addAttribute("username", username);
        return "admin/addCollege";
    }


    // ajax校验部门编号是否存在
    @RequestMapping(value = "/checkcollegeID.html")
    @ResponseBody
    public String checkcollegeID(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        String collegeID = request.getParameter("collegeID");
        College college = new College();
        college.setCollegeid(Integer.parseInt(collegeID));
        Boolean result = adminService.checkcollegeID("Admin.checkcollegeID", college);
        //学号存在时
        if (result) {
            return "exist";
        }
        //学号不存在
        return "unexist";
    }

    // 添加部门信息的保存
    @RequestMapping("/addCollegeed.html")
    public String addCollegeed(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {
        String username = request.getParameter("username");
        String collegeID = request.getParameter("collegeID");
        String collegeName = request.getParameter("collegeName");
        String salary = request.getParameter("salary");
        College college = new College();
        college.setCollegeid(Integer.parseInt(collegeID));
        college.setCollegename(collegeName);
        college.setSalary(salary);
        adminService.addCollegeed("Admin.addCollegeed", college);
        model.addAttribute("username", username);
        return "redirect:showCollege.html";
    }

    // 进入修改部门信息处理页面
    @RequestMapping(value = "/editCollege.html", method = {RequestMethod.GET})
    public String editCollege(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String username = request.getParameter("username");
        String collegeID = request.getParameter("collegeID");
        String collegeName = request.getParameter("collegeName");
        String salary = request.getParameter("salary");
        model.addAttribute("username", username);
        model.addAttribute("collegeID", collegeID);
        model.addAttribute("collegeName", collegeName);
        model.addAttribute("salary", salary);
        return "admin/editCollege";
    }

    // 修改部门信息的保存
    @RequestMapping(value = "/editCollegeed.html", method = {RequestMethod.POST})
    public String editCollegeed(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String username = request.getParameter("username");
        model.addAttribute("username", username);
        String collegeID = request.getParameter("collegeID");
        String collegeName = request.getParameter("collegeName");
        String salary = request.getParameter("salary");
        College college = new College();
        college.setCollegeid(Integer.parseInt(collegeID));
        college.setCollegename(collegeName);
        college.setSalary(salary);
        adminService.updateCollege("Admin.updateCollege", college);
        //重定向
        return "redirect:showCollege.html";
    }

    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<考勤管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
    // 考勤页面显示
    @RequestMapping("/showCheckingin.html")
    public String showCheckingin(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {

        return "admin/showCheckingin";
    }

    @RequestMapping("/showCheckinginSelect.html")
    public String showCheckinginSelect(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        String type = null;
        String findByName = null;
        String username = request.getParameter("username");
        type = request.getParameter("type");
        findByName = request.getParameter("findByName");
        map.put("type", type);
        map.put("findByName", findByName);

        if (type != null && !"".equals(type) && findByName != null && !"".equals(findByName)) {
            List<Salary> list = adminService.findCheckinginByNameType("Admin.findCheckinginByNameType", map);
            model.addAttribute("list", list);
            model.addAttribute("type", type);
            model.addAttribute("username", username);
            return "admin/showCheckingin";
        }

        if ((type == null || "".equals(type)) && findByName != null && !"".equals(findByName)) {
            List<Salary> list = adminService.findCheckinginByName("Admin.findCheckinginByName", map);
            model.addAttribute("list", list);
            model.addAttribute("type", type);
            model.addAttribute("username", username);
            return "admin/showCheckingin";
        }
        if ((findByName == null || "".equals(findByName)) && type != null && !"".equals(type)) {
            List<Salary> list = adminService.findCheckinginByType("Admin.findCheckinginByType", map);
            model.addAttribute("list", list);
            model.addAttribute("type", type);
            model.addAttribute("username", username);
            return "admin/showCheckingin";
        }
        return "admin/showCheckingin";
    }

    // 添加考勤
    @RequestMapping("/addCheckingin.html")
    public String addCheckingin(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {
        String username = request.getParameter("username");
        model.addAttribute("username", username);
        return "admin/addCheckingin";
    }

    // 添加考勤保存
    @RequestMapping("/addCheckingined.html")
    public String addCheckingined(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {
        String username = request.getParameter("username");
        String teacherID = request.getParameter("teacherID");
        String type = request.getParameter("type");
        String time = request.getParameter("time");
        String state = request.getParameter("state");
        String money = request.getParameter("money");
        Salary salary = new Salary();
        salary.setTeacherID(teacherID);
        salary.setType(type);
        salary.setTime(time);
        salary.setState(state);
        salary.setMoney(money);
        adminService.addCheckingin("Admin.addCheckingin", salary);
        model.addAttribute("username", username);
        return "redirect:showCheckingin.html";
    }

    // 进入修改考勤信息处理页面
    @RequestMapping(value = "/editCheckingin.html", method = {RequestMethod.GET})
    public String editCheckingin(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String teacherID = request.getParameter("teacherID");
        String time = request.getParameter("time");
        String state = request.getParameter("state");
        String money = request.getParameter("money");
        model.addAttribute("id", id);
        model.addAttribute("username", username);
        model.addAttribute("teacherID", teacherID);
        model.addAttribute("time", time);
        model.addAttribute("state", state);
        model.addAttribute("money", money);
        return "admin/editCheckingin";
    }

    // 保存修改的信息
    @RequestMapping(value = "/editCheckingined.html")
    public String editCheckingined(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String teacherID = request.getParameter("teacherID");
        String time = request.getParameter("time");
        String state = request.getParameter("state");
        String money = request.getParameter("money");
        Salary salary = new Salary();
        salary.setId(id);
        salary.setTime(time);
        salary.setState(state);
        salary.setMoney(money);
        adminService.editCheckingined("Admin.editCheckingined", salary);
        return "admin/showCheckingin";
    }

    // 删除考勤信息
    @RequestMapping(value = "/removeCheckingin.html")
    public String removeCheckingin(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String id = request.getParameter("id");
        Salary salary = new Salary();
        salary.setId(id);
        adminService.removeCheckingin("Admin.removeCheckingin", salary);
        return "admin/showCheckingin";
    }
    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<请假管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

    // 教师页面显示
    @RequestMapping("/showVacate.html")
    public String showVacate(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {

        Integer curpage = 1;//从第一页开始
        Integer perpage = 10;//每页两条数据
        if (request.getParameter("curpage") != null) {
            curpage = Integer.parseInt(request.getParameter("curpage"));
        }
        ;//此处获取点击的页数

        String findByName = null;
        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("curpage", (curpage - 1) * perpage);
        map.put("perpage", perpage);

        if (!"".equals(request.getParameter("findByName")) && request.getParameter("findByName") != null) {
            findByName = request.getParameter("findByName");
            map.put("findByName", findByName);
            List<Teacher> list = adminService.findByNameVacate("Admin.findByNameVacate", map);
            //查询数据库中总条数
            Integer totalcount = adminService.getCountByNameVacate("Admin.getCountVacate", map);
            model.addAttribute("list", list);
            model.addAttribute("username", username);
            return "admin/showVacate";
        }

        List<Teacher> list = adminService.findByPagingVacate("Admin.findByPagingVacate", map);
        model.addAttribute("list", list);
        //查询数据库中总条数
        Integer totalcount = adminService.getCountVacate("Admin.getCountVacate", map);

        Integer totalpage = 0;
        if (totalcount % perpage == 0) {
            totalpage = totalcount / perpage;
        } else {
            totalpage = (totalcount / perpage) + 1;
        }

        model.addAttribute("totalcountStudent", totalcount);
        model.addAttribute("list", list);
        model.addAttribute("curpage", curpage);
        model.addAttribute("totalpage", totalpage);
        model.addAttribute("findByName", findByName);
        model.addAttribute("username", username);

        return "admin/showVacate";
    }

    // 更新状态
    @RequestMapping(value = "/editVacate.html")
    public String editVacate(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String id = request.getParameter("id");
        String state = request.getParameter("state");
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setState(state);
        adminService.editVacate("Admin.editVacate", teacher);
        return "redirect:showVacate.html";
    }

    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<奖金管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

    // 教师页面显示
    @RequestMapping("/showBonus.html")
    public String showBonus(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {


        Integer curpage = 1;//从第一页开始
        Integer perpage = 10;//每页两条数据
        if (request.getParameter("curpage") != null) {
            curpage = Integer.parseInt(request.getParameter("curpage"));
        }
        ;//此处获取点击的页数

        String findByName = null;
        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("curpage", (curpage - 1) * perpage);
        map.put("perpage", perpage);

        if (!"".equals(request.getParameter("findByName")) && request.getParameter("findByName") != null) {
            findByName = request.getParameter("findByName");
            map.put("findByName", findByName);
            List<Teacher> list = adminService.findByNameBonus("Admin.findByNameBonus", map);
            //查询数据库中总条数
            Integer totalcount = adminService.getCountByNameBonus("Admin.getCountVacate", map);
            model.addAttribute("list", list);
            model.addAttribute("username", username);
            return "admin/showBonus";
        }

        List<Teacher> list = adminService.findByPagingBonus("Admin.findByPagingBonus", map);
        model.addAttribute("list", list);
        //查询数据库中总条数
        Integer totalcount = adminService.getCountBonus("Admin.getCountBonus", map);

        Integer totalpage = 0;
        if (totalcount % perpage == 0) {
            totalpage = totalcount / perpage;
        } else {
            totalpage = (totalcount / perpage) + 1;
        }

        model.addAttribute("totalcountStudent", totalcount);
        model.addAttribute("list", list);
        model.addAttribute("curpage", curpage);
        model.addAttribute("totalpage", totalpage);
        model.addAttribute("findByName", findByName);
        model.addAttribute("username", username);

        return "admin/showBonus";

    }

    //添加奖金页面
    @RequestMapping("/addBonus.html")
    public String addBonus(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        return "admin/addBonus";
    }

    //添加奖金页面的保存
    @RequestMapping("/addBonused.html")
    public String addBonused(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String username = request.getParameter("username");
        String teacherID = request.getParameter("teacherID");
        String time = request.getParameter("time");
        String bonus = request.getParameter("bonus");
        String reason = request.getParameter("reason");
        Teacher teacher =new Teacher();
        teacher.setTeacherID(teacherID);
        teacher.setTime(time);
        teacher.setBonus(bonus);
        teacher.setReason(reason);
        adminService.addBonused("Admin.addBonused", teacher);
        model.addAttribute("username", username);
        return "redirect:showBonus.html";
    }

    // 进入修改考勤信息处理页面
    @RequestMapping(value = "/editBonus.html", method = {RequestMethod.GET})
    public String editBouns(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String teacherID = request.getParameter("teacherID");
        String time = request.getParameter("time");
        String reason = request.getParameter("reason");
        String bonus = request.getParameter("bonus");
        model.addAttribute("id", id);
        model.addAttribute("username", username);
        model.addAttribute("teacherID", teacherID);
        model.addAttribute("time", time);
        model.addAttribute("reason", reason);
        model.addAttribute("bonus", bonus);
        return "admin/editBonus";
    }

    // 保存修改的信息
    @RequestMapping(value = "/editBonused.html")
    public String editBonused(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String teacherID = request.getParameter("teacherID");
        String time = request.getParameter("time");
        String reason = request.getParameter("reason");
        String bonus = request.getParameter("bonus");
        Teacher teacher =new Teacher();
        teacher.setId(id);
        teacher.setTeacherID(teacherID);
        teacher.setTime(time);
        teacher.setReason(reason);
        teacher.setBonus(bonus);
        System.out.println(teacher);
        adminService.editBonused("Admin.editBonused", teacher);
        model.addAttribute("username", username);
        return "redirect:showBonus.html";
    }
    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<合同管理>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

    // 合同页面显示
    @RequestMapping("/showContract.html")
    public String showContract(HttpServletRequest request, HttpServletResponse response, Model model, Integer page) throws Exception {


        Integer curpage = 1;//从第一页开始
        Integer perpage = 10;//每页两条数据
        if (request.getParameter("curpage") != null) {
            curpage = Integer.parseInt(request.getParameter("curpage"));
        }
        ;//此处获取点击的页数

        String findByName = null;
        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("curpage", (curpage - 1) * perpage);
        map.put("perpage", perpage);

        if (!"".equals(request.getParameter("findByName")) && request.getParameter("findByName") != null) {
            findByName = request.getParameter("findByName");
            map.put("findByName", findByName);
            List<Teacher> list = adminService.findByNameTeacher("Admin.findByNameTeacher", map);
            model.addAttribute("list", list);
            model.addAttribute("username", username);
            return "admin/showContract";
        }

        List<Teacher> list = adminService.findByPagingTeacher("Admin.findByPagingTeacher", map);
        model.addAttribute("list", list);
        //查询数据库中总条数
        Integer totalcount = adminService.getCountTeacher("Admin.getCountTeacher", map);

        Integer totalpage = 0;
        if (totalcount % perpage == 0) {
            totalpage = totalcount / perpage;
        } else {
            totalpage = (totalcount / perpage) + 1;
        }

        model.addAttribute("totalcountStudent", totalcount);
        model.addAttribute("list", list);
        model.addAttribute("curpage", curpage);
        model.addAttribute("totalpage", totalpage);
        model.addAttribute("findByName", findByName);
        model.addAttribute("username", username);

        return "admin/showContract";

    }

    // 进入修改合同信息处理页面
    @RequestMapping(value = "/editContract.html", method = {RequestMethod.GET})
    public String editContract(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String teacherID = request.getParameter("teacherID");
        String teacherName = request.getParameter("teacherName");
        String contract = request.getParameter("contract");
        String title = request.getParameter("title");
        String grade = request.getParameter("grade");
        String collegeid = request.getParameter("collegeid");
        model.addAttribute("teacherID", teacherID);
        model.addAttribute("teacherName", teacherName);
        model.addAttribute("contract", contract);
        model.addAttribute("title", title);
        model.addAttribute("grade", grade);
        model.addAttribute("collegeid", collegeid);
        return "admin/editContract";
    }

    // 修改老师信息的保存
    @RequestMapping(value = "/editContracted.html")
    public String editContracted( @RequestParam("file") CommonsMultipartFile files[],HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String teacherID = request.getParameter("teacherID");
        String teacherName = request.getParameter("teacherName");
        String title = request.getParameter("title");
        String grade = request.getParameter("grade");
        String collegeid = request.getParameter("collegeid");
        Teacher teacher = new Teacher();
        teacher.setTeacherID(teacherID);
        teacher.setTeachername(teacherName);
        teacher.setTitle(title);
        teacher.setGrade(grade);
        teacher.setCollegeName(collegeid);
        List<String> list = new ArrayList<String>();
        // 获得项目的路径
        ServletContext sc = request.getSession().getServletContext();
        // 上传位置
        String path ="D:\\Teacher_Salary\\web\\contract\\";
        File f = new File(path);
        if (!f.exists())
            f.mkdirs();
        for (int i = 0; i < files.length; i++) {
            // 获得原始文件名
            String fileName = files[i].getOriginalFilename();
            // 新文件名
            String newFileName = UUID.randomUUID() + fileName;
            if (!files[i].isEmpty()) {
                try {
                    FileOutputStream fos = new FileOutputStream(path
                            + newFileName);
                    InputStream in = files[i].getInputStream();
                    int b = 0;
                    while ((b = in.read()) != -1) {
                        fos.write(b);
                    }
                    fos.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            teacher.setContract(newFileName);
            adminService.updateContract("Admin.updateContract", teacher);
        }
        //重定向
        return "redirect:showContract.html";
    }

}

